﻿using System.Linq;
using System.Web;
using BusinessLogic.Caching;
using System.Collections.Generic;
using BusinessLogic.Data;
using Common;
using Common.Utils;
using BusinessLogic.Infrastructure;
using BusinessLogic.Audit;
using System;

namespace BusinessLogic.Messages
{
    public partial class ResultsService : IResultsService
    {
        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly DataMailDataContext _context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public ResultsService(DataMailDataContext context)
        {
            this._context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion
        /// <summary>
        /// </summary>
        /// <param name="ResultsId"></param>
        /// <exception cref="System.NotImplementedException"></exception>
        public void DeleteResult(int ResultsId)
        {
            throw new System.NotImplementedException();
        }
        /// <summary>
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        public int Save(List<Result> List)
        {
            try
            {
                _context.Results.InsertAllOnSubmit(this.Tranfer(List));
                _context.SubmitChanges();
            }
            catch (Exception ex)
            {
                IoC.Resolve<ILogService>().InsertLog(LogTypeEnum.Result, "Save to result", ex.Message);
            }
            return List.Count;
        }
        #region Utilities
        /// <summary>
        /// Tranfers the specified list.
        /// </summary>
        /// <param name="List">The list.</param>
        /// <returns></returns>
        private List<BusinessLogic.Data.Result> Tranfer(List<Result> List, int MessageID = 0)
        {
            List<Data.Result> ListResult = new List<Data.Result>();
            foreach (var result in List)
            {
                ListResult.Add(new Data.Result
                {
                    PackageCode = CommonHelper.Truncate(result.PackageCode, 256),
                    DatePurchase = CommonHelper.Truncate(result.DatePurchase, 10),
                    CustomerName = CommonHelper.Truncate(result.CustomerName, 256),
                    TaxCode = CommonHelper.Truncate(result.TaxCode, 64),
                    Address = CommonHelper.Truncate(result.Address, 256),
                    Mobile = CommonHelper.Truncate(result.Mobile, 64),
                    Email = CommonHelper.Truncate(result.Email, 64),
                    ServiceFree = CommonHelper.Truncate(result.ServiceFree, 128),
                    CustomerProfile = CommonHelper.Truncate(result.CustomerProfile, 128),
                    ResConfirm = result.ResConfirm,
                    ResStatus = result.ResStatus,
                    MessageID = MessageID
                });
            }
            return ListResult;
        }
        #endregion
        /// <summary>
        /// Saves the message.
        /// </summary>
        /// <param name="messgae">The messgae.</param>
        /// <returns></returns>
        private void SaveMessage(MessageMails messgae)
        {
            var item = new MessageMail
               {
                   MessageID = messgae.MessageID,
                   Frommail = messgae.Frommail,
                   ResDateReceive = messgae.ResDateReceive,
                   Body = messgae.Body,
                   DateSent = messgae.DateSent,
                   attachment = messgae.attachment,
                   Modified = messgae.Modified,
                   Createby = messgae.Createby,
                   CreateDate = messgae.CreateDate,
                   ModifiedDate = messgae.ModifiedDate,
                   Subject = messgae.Subject,
                   Confirm = 3
               };
            _context.MessageMails.InsertOnSubmit(item);
            _context.SubmitChanges();
            _context.Results.InsertAllOnSubmit(this.Tranfer(messgae.Results, item.MessageID));
            _context.SubmitChanges();
        }
        public void Save(MessageMails message)
        {
            using (var trans = new System.Transactions.TransactionScope())
            {
                try
                {
                    this.SaveMessage(message);

                    trans.Complete();
                }
                catch
                {
                    trans.Dispose();
                }
            }
        }
    }
}
